// pages/index-new/index.js
const rr = require("../../utils.js");
let navigationBar;
let head;
let catGoods;
let goodsList;
Page({
  data: {
    shop_id: -1,
    block_list: [],
    activity_list: [],
    goods_num:1,
    activity_status: 0,
    loadingStatus: 0,
    page: 1,
    goods_list: null,
    is_diy: 1,
    page_id: 0
  },

  onLoad: function (t) {
    getApp().pageOnLoad(this, t);
    var that = this;
    wx.getSystemInfo({
      success (res) {
        wx.setStorageSync('headheight', res.statusBarHeight)
        that.setData({
            headheight: res.statusBarHeight
        })
      }
    })
    var shop = wx.getStorageSync('address_info');
    var shop_id = shop && shop.shop_id ? shop.shop_id : 0
    this.setData({
      shop_id: shop_id
    })
    
    if (t.scene) {
      var m = decodeURIComponent(t.scene);
      var hh = rr.scene_decode(m);
      if (hh.page_id) {
        this.data.page_id = hh.page_id;
      }
      wx.setStorageSync("scene", hh)
      this.updateShop(hh);
    } else {
      if (t.page_id) {
        this.data.page_id = t.page_id;
      }
      this.getDiyMode(t);
    }
  },
  // 切换社区
  updateShop(hh) {
    var thst = this;
    if (hh.shop_id != '' && hh.shop_id != undefined && hh.shop_id != null && hh.shop_id != 0) {
      var shop = wx.getStorageSync('address_info');
      var shop_id = shop && shop.shop_id ? shop.shop_id : 0
      if (shop_id == 0 && hh.shop_id > 0) {
        this.updateShopSubimt(hh.shop_id);
      }else if(shop_id > 0 && hh.shop_id > 0 && shop_id != hh.shop_id && thst.data.store.update_shop ) {
        getApp().request({
          url: getApp().api.default.index_position,
          data: {
            tz_id: hh.shop_id,
          },
          method: "POST",
          success: function (t) {
            wx.showModal({
              title: '提示',
              content: '是否切换到' + t.name + '社区',
              success: function (res) {
                if (res.confirm) {
                  wx.removeStorage({
                    key: 'address_info'
                  })
                  thst.updateShopSubimt(hh.shop_id)
                }
                thst.getDiyMode(t);
              }
            })
            return false;
          },
          
        })
      }else {
        thst.getDiyMode();
      }
    } else {
      thst.getDiyMode();
    }
  },
  // 确认切换社区
  updateShopSubimt: function (shop_id) {
    var that = this;
    getApp().request({
        url: getApp().api.default.index_position,
        data: {
          tz_id: shop_id,
          is_update: 1,
        },
        method: "POST",
        success: function (t) {
          wx.setStorage({
            key: "address_info",
            data: {
              nickname: t.nickname,
              name: t.name,
              address: t.address,
              logo: t.pic_url,
              shop_id: t.id,
              peisong_type: t.peisong_type,
              mobile: t.mobile
            }
          })
          
          return false;
        },
        complete: function (res) {
          wx.reLaunch({
            url: "/pages/index/index",
          })
        }
    })
  },
  // 判断是否已经添加diy首页
  getDiyMode: function () {
    var that = this;
    that.getDiyData()
    
  },
  getDiyData: function () {
    var that = this;
    wx.showLoading({
      title: "加载中"
    })
    var shop = wx.getStorageSync('address_info');
    this.setData({
      shop_id: shop && shop.shop_id ? shop.shop_id : 0
    })
    getApp().request({
      url: getApp().api.diy.diy_info,
      data: {
        shop_id: that.data.shop_id,
        page_id: that.data.page_id
      },
      success: function(t) {
        if (t.code == 0) {
          that.setData({
            template: t.data.template
          })
        }
      },
      complete: function() {
        setTimeout(function() {
          navigationBar = that.selectComponent("#navigationBar")
          catGoods = that.selectComponent("#catGoods")
        },1000)
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().pageOnShow(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    wx.reLaunch({
      url: '/pages/diy/index?page_id=' + that.data.page_id,
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (catGoods) {
      catGoods.getGoodsList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var i = this;
    var shop_id = i.data.shop_id ? i.data.shop_id : 0;
    var user_id = wx.getStorageSync("user_info").id ? wx.getStorageSync("user_info").id : 0;
    return {
      // path: "/pages/index/index?user_id=" + wx.getStorageSync("user_info").id,
      path: "/pages/diy/index?scene=shop_id:" + shop_id + ",user_id:"+  user_id+ ",page_id:"+  i.data.page_id,
      imageUrl: i.data.store.share_bg,
      title: i.data.store.name ,
      success: function (t) {
        
      },
    };
  },
  onShareTimeline: function () {
    var i = this;
    var shop_id = i.data.shop_id ? i.data.shop_id : 0;
    var user_id = wx.getStorageSync("user_info").id ? wx.getStorageSync("user_info").id : 0;
    return {
      query: "scene=shop_id:" + shop_id + ",user_id:"+  user_id+ ",page_id:"+  i.data.page_id,
      imageUrl: i.data.store.share_bg,
      title: i.data.store.name ,
      success: function (t) {
        
      },
    };
  },
  // 页面滚动
  onPageScroll: function (e) {
    var that=this;
    if (!navigationBar && !head) {
      navigationBar = that.selectComponent("#navigationBar")
      catGoods = that.selectComponent("#catGoods")
      goodsList = that.selectComponent("#goodsList")
      head = that.selectComponent("#head")
    }else {
      if (this.data.is_diy == 1) {
        var scrollTop = e.scrollTop
        if (navigationBar) {
          navigationBar.setOpacity(scrollTop, 200)
        }else if(head) {
          head.setOpacity(scrollTop);
        }
      }
    }
  },
  goodsQrcodeClose: function() {
    this.setData({
      goods_qrcode_active: "",
      no_scroll: !1
    });
  },
  // 显示分享
  showShareModal: function() {
    this.setData({
      share_modal_active: "active",
      no_scroll: !0
    });
  },
  shareModalClose: function() {
    this.setData({
      share_modal_active: "",
      no_scroll: !1
    });
  },
  tabberToIndex:function(){
    wx.switchTab({ url:"/pages/index/index" })
  },
})